package one.stand.controller.supplier;

import cn.hutool.core.date.DateUtil;
import com.jmp.api.dto.VideoMyListResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.DynamicApi;
import com.jmp.base.LiveApi;
import com.jmp.base.dto.LiveListRequest;
import com.jmp.base.dto.LiveListVo;
import com.jmp.feign.LiveFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.enums.SeeRecordEnums;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.RecordPublicService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cjr
 */
@Api(description = "视频管理")
@RestController
@RequestMapping("video/my")
public class VideoMyController {
    @Autowired
    private LiveFeign liveFeign;

    @Autowired
    private RecordPublicService recordPublicService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;

    @ApiOperation(value = "1大厅")
    @GetMapping("list")
    @SecurityAuthCheck
    public ResultModel<VideoMyListResponse> list(@RequestAttribute PageRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        BusinessCard businessCard = businessCardBaseService.getCustomer(optUser.getCardId());

        LiveListRequest liveVideoListDto = LiveListRequest.builder()
                .cardId(optUser.getCardId())
                .videoType(LiveApi.VIDEO_TYPE_VIDEO)
                .build();
        liveVideoListDto.setPageInit(request.getPageNum(), request.getPageSize());
        ResultModel<LiveListVo> resultModel = liveFeign.list(liveVideoListDto);
        AssertUtil.isTrue(resultModel);
        PageResponse2<VideoMyListResponse.Video> videoPageResponse2 = ConvertUtils.page2(resultModel.getData().getLivePage(), video -> {
            Long viewNum = recordPublicService.getPopularity(SeeRecordEnums.LIVE.getCode(), video.getRoomId());
            Integer collectNum = recordPublicService.getCollectionCount(RecordPublicService.ITEMTYPE_LIVE_ROOM, video.getRoomId());
            Integer commentNum = recordPublicService.countCommentByType(DynamicApi.COMMENT_TYPE_VIDEO, video.getRoomId());
            return VideoMyListResponse.Video.builder()
                    .roomId(video.getRoomId())
                    .photo(video.getPhoto())
                    .name(video.getTitle())
                    .time(video.getTime().getTime())
                    .videoDuration(DateUtil.secondToTime(video.getVideoDuration()))

                    .viewNum(viewNum)
                    .collectNum(collectNum)
                    .commentNum(commentNum)

                    .video(VideoMyListResponse.VideoDtail.builder()
                            .roomId(video.getRoomId())
                            .name(video.getTitle())
                            .photo(video.getPhoto())
                            .videoUrl(video.getReplayUrl())
                            .userCurrentRoomId(video.getUserCurrentRoomId())

                            .cardId(businessCard.getCardId())
                            .cardImg(businessCard.getLogo())
                            .cardName(businessCard.getName())
                            .userId(businessCard.getUserId())
                            .userCollectFlag(BooleanUtils.toInteger(recordPublicService.isCollection(optUser.getUserId(), RecordPublicService.ITEMTYPE_USER, businessCard.getUserId())))

                            .viewNum(viewNum)
                            .collectNum(collectNum)
                            .collectFlag(BooleanUtils.toInteger(recordPublicService.isCollection(optUser.getUserId(), RecordPublicService.ITEMTYPE_LIVE_ROOM, video.getRoomId())))
                            .commentNum(commentNum)
                            .build())
                    .build();
        });

        VideoMyListResponse response = VideoMyListResponse.builder()
                .videoPage(videoPageResponse2)
                .build();
        return ResultModel.success(response);
    }
}
